<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
    <link href="css/hiprint.css" rel="stylesheet" />
    <link href="css/print-lock.css" rel="stylesheet" />

    <link href="content/bootstrap.min.css" rel="stylesheet" />
    <script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
    <script src="content/bootstrap.min.js"></script>

    <style>
      .hinnn-layout,
      .hinnn-layout * {
        box-sizing: border-box;
      }

      .hinnn-layout {
        display: flex;
        flex: auto;
        flex-direction: column;
      }

      .hinnn-layout.hinnn-layout-has-sider {
        flex-direction: row;
      }

      .hinnn-layout-sider {
        display: flex;
        flex-direction: row;
        position: relative;
      }

      .hinnn-layout-content {
        flex: auto;
      }

      .hinnn-header {
        position: relative;

        z-index: 1030;
        display: block;
      }

      .wrapper {
        min-height: 100%;
      }

      .height-100-per {
        height: 100%;
      }
    </style>
  </head>

  <body>
    <!--[[ 测试专用  单独使用无需引入  -->
    <script src="custom_test/custom-etype-provider.js"></script>
    <script src="custom_test/custom-print-json.js"></script>
    <script src="custom_test/print-data.js"></script>
    <!--测试专用  单独使用无需引入 ]]  -->
    <!--单独使用无需引入  -->
    <script src="polyfill.min.js"></script>
    <script src="plugins/jquery.minicolors.min.js"></script>
    <script src="plugins/JsBarcode.all.min.js"></script>
    <script src="plugins/qrcode.js"></script>
    <script src="hiprint.bundle.js"></script>
    <script src="plugins/jquery.hiwprint.js"></script>

    <button class="getJsonTid" onclick="getJsonTid()">getJsonTid</button>
    <button class="getJsonTid" onclick="yulan()">预览</button>
    <button class="dayin" onclick="dayin()">打印</button>
    <div class="hiprintEpContainer"></div>
    <div id="printTemplate"></div>
    <div id="printElementOptionSetting"></div>
    <div id="yulan"></div>

    <script>
      let abc2 = [
        {
          id: "create_uid/complete_name",
          string: "创建人/全名",
          field_type: "char",
        },
        { id: "create_date", string: "创建日期", field_type: "datetime" },
        {
          id: "company_id/name",
          string: "公司/公司名称",
          value: "company_id/name",
          children: false,
          field_type: "char",
          required: true,
          relation_field: null,
          default_export: false,
          parent: {
            id: "company_id",
            string: "公司",
            value: "company_id/id",
            children: true,
            field_type: "many2one",
            required: true,
            relation_field: null,
            default_export: false,
            params: {
              model: "res.company",
              prefix: "company_id",
              name: "公司",
              parent_field: {
                default_export_compatible: false,
                relation: "res.company",
                required: true,
                string: "公司",
                type: "many2one",
              },
            },
          },
        },
        {
          id: "order_line/product_id/name",
          string: "订单行/产品/名称",
          value: "order_line/product_id/name",
          children: false,
          field_type: "char",
          required: true,
          relation_field: null,
          default_export: false,
          parent: {
            id: "order_line/product_id",
            string: "订单行/产品",
            value: "order_line/product_id/id",
            children: true,
            field_type: "many2one",
            required: false,
            relation_field: null,
            default_export: false,
            params: {
              model: "product.product",
              prefix: "order_line/product_id",
              name: "订单行/产品",
              parent_field: {
                default_export_compatible: false,
                relation: "product.product",
                required: false,
                string: "产品",
                type: "many2one",
              },
            },
            parent: {
              id: "order_line",
              string: "订单行",
              value: "order_line/id",
              children: true,
              field_type: "one2many",
              required: false,
              relation_field: "order_id",
              default_export: false,
              params: {
                model: "sale.order.line",
                prefix: "order_line",
                name: "订单行",
                parent_field: {
                  default_export_compatible: false,
                  relation: "sale.order.line",
                  relation_field: "order_id",
                  required: false,
                  string: "订单行",
                  type: "one2many",
                },
              },
            },
          },
        },
        {
          id: "order_line/qty_delivered",
          string: "订单行/交付数量",
          value: "order_line/qty_delivered",
          children: false,
          field_type: "float",
          required: false,
          relation_field: null,
          default_export: false,
          parent: {
            id: "order_line",
            string: "订单行",
            value: "order_line/id",
            children: true,
            field_type: "one2many",
            required: false,
            relation_field: "order_id",
            default_export: false,
            params: {
              model: "sale.order.line",
              prefix: "order_line",
              name: "订单行",
              parent_field: {
                default_export_compatible: false,
                relation: "sale.order.line",
                relation_field: "order_id",
                required: false,
                string: "订单行",
                type: "one2many",
              },
            },
          },
        },
        {
          id: "order_line/price_unit",
          string: "订单行/单价",
          value: "order_line/price_unit",
          children: false,
          field_type: "float",
          required: true,
          relation_field: null,
          default_export: false,
          parent: {
            id: "order_line",
            string: "订单行",
            value: "order_line/id",
            children: true,
            field_type: "one2many",
            required: false,
            relation_field: "order_id",
            default_export: false,
            params: {
              model: "sale.order.line",
              prefix: "order_line",
              name: "订单行",
              parent_field: {
                default_export_compatible: false,
                relation: "sale.order.line",
                relation_field: "order_id",
                required: false,
                string: "订单行",
                type: "one2many",
              },
            },
          },
        },
        {
          id: "order_line/price_subtotal",
          string: "订单行/小计",
          value: "order_line/price_subtotal",
          children: false,
          field_type: "monetary",
          required: false,
          relation_field: null,
          default_export: false,
          parent: {
            id: "order_line",
            string: "订单行",
            value: "order_line/id",
            children: true,
            field_type: "one2many",
            required: false,
            relation_field: "order_id",
            default_export: false,
            params: {
              model: "sale.order.line",
              prefix: "order_line",
              name: "订单行",
              parent_field: {
                default_export_compatible: false,
                relation: "sale.order.line",
                relation_field: "order_id",
                required: false,
                string: "订单行",
                type: "one2many",
              },
            },
          },
        },
      ];
      let abc1 = [
        {
          id: "warehouse_id",
          string: "仓库",
          value: "warehouse_id/id",
          children: true,
          field_type: "many2one",
          required: false,
          relation_field: null,
          default_export: false,
          params: {
            model: "stock.warehouse",
            prefix: "warehouse_id",
            name: "仓库",
            parent_field: {
              default_export_compatible: false,
              relation: "stock.warehouse",
              required: false,
              string: "仓库",
              type: "many2one",
            },
          },
        },
        {
          id: "company_id",
          string: "公司",
          value: "company_id/id",
          children: true,
          field_type: "many2one",
          required: true,
          relation_field: null,
          default_export: false,
          params: {
            model: "res.company",
            prefix: "company_id",
            name: "公司",
            parent_field: {
              default_export_compatible: false,
              relation: "res.company",
              required: true,
              string: "公司",
              type: "many2one",
            },
          },
        },
        {
          id: "create_date",
          string: "创建日期",
          value: "create_date",
          children: false,
          field_type: "datetime",
          required: false,
          relation_field: null,
          default_export: false,
        },
        {
          id: "partner_id",
          string: "客户",
          value: "partner_id/id",
          children: true,
          field_type: "many2one",
          required: true,
          relation_field: null,
          default_export: false,
          params: {
            model: "res.partner",
            prefix: "partner_id",
            name: "客户",
            parent_field: {
              default_export_compatible: false,
              relation: "res.partner",
              required: true,
              string: "客户",
              type: "many2one",
            },
          },
        },
        {
          id: "amount_total",
          string: "总计",
          value: "amount_total",
          children: false,
          field_type: "monetary",
          required: false,
          relation_field: null,
          default_export: false,
        },
        {
          id: "activity_ids",
          string: "活动",
          value: "activity_ids/id",
          children: true,
          field_type: "one2many",
          required: false,
          relation_field: "res_id",
          default_export: false,
          params: {
            model: "mail.activity",
            prefix: "activity_ids",
            name: "活动",
            parent_field: {
              default_export_compatible: false,
              relation: "mail.activity",
              relation_field: "res_id",
              required: false,
              string: "活动",
              type: "one2many",
            },
          },
        },
        {
          id: "state",
          string: "状态",
          value: "state",
          children: false,
          field_type: "selection",
          required: false,
          relation_field: null,
          default_export: false,
        },
        {
          id: "website_id",
          string: "网站",
          value: "website_id/id",
          children: true,
          field_type: "many2one",
          required: false,
          relation_field: null,
          default_export: false,
          params: {
            model: "website",
            prefix: "website_id",
            name: "网站",
            parent_field: {
              default_export_compatible: false,
              relation: "website",
              required: false,
              string: "网站",
              type: "many2one",
            },
          },
        },
        {
          id: "name",
          string: "订单参考号",
          value: "name",
          children: false,
          field_type: "char",
          required: true,
          relation_field: null,
          default_export: false,
        },
        {
          id: "user_id",
          string: "销售人员",
          value: "user_id/id",
          children: true,
          field_type: "many2one",
          required: false,
          relation_field: null,
          default_export: false,
          params: {
            model: "res.users",
            prefix: "user_id",
            name: "销售人员",
            parent_field: {
              default_export_compatible: false,
              relation: "res.users",
              required: false,
              string: "销售人员",
              type: "many2one",
            },
          },
        },
      ];
      const configData = transformFields(abc1);
      console.log(configData);
      var printData = {
        name_abc: "黄磊",
        gender: "男",
        test: { a: "aaaa" },
        like: "读书写字",
        email: "hinnn.com@gmail.com",
        address: "北京朝阳区二道口",
        phone: "18888888888",
        target: "产品数据分析师",
        workExperience: [
          {
            position: "数据分析师",
            company: "某宝",
            address: "北京",
            date: "四月-2010-八月-2011",
            work: "专题分析/模型，根据业务分析分析各类专题，例如：商品等级，会员消费行为营销活动，重要产品数据等。",
          },
          {
            position: "数据分析师",
            company: "某东",
            address: "北京",
            date: "九月-2011-三月-2016",
            work: "数据指标监控，根据产品，运营以及管理的需求，针对产品数据，运营，业务进展及KPI指标的监控，经营分析，对口BU的经营报告，分析KPI进度，交易产品异动原因，分析市场动态，汇报对象为BU经理。",
          },
          {
            position: "数据产品经理",
            company: "大米",
            address: "北京",
            date: "四月-2016-至今",
            work: "负责具体分析项目，例如实时监控，数据后台管理，协调开发资源，控制项目进度。",
          },
        ],
        professional: "信息管理与信息系统",
        university: "北京邮电大学",
        universityAddress: "北京朝阳区二道口",
        universityDate: "2010",
        tech: "MYSQL,HIVE(数据仓库工具),SPSS(统计产品已服务解决方案),数据挖掘，跨部门沟通能力，业务理解能力，数据解读分析。",
      };
      let shuju = [
        {
          id: 571,
          locked: false,
          authorized_transaction_ids: [],
          review_ids: [
            {
              id: 14288,
              name: "1111",
              sequence: 1,
              requested_by: { id: 2, display_name: "Mitchell Admin" },
              status: "approved",
              display_status: "已批准",
              todo_by: "Mitchell Admin",
              done_by: { id: 2, display_name: "Mitchell Admin" },
              reviewed_date: "2025-07-21 12:42:57",
              reviewed_formated_date: "2025-07-21 20:42:57",
              comment: false,
            },
          ],
          hide_reviews: true,
          rejected: false,
          need_validation: false,
          state: "sale",
          partner_credit_warning: "",
          duplicated_order_ids: [],
          next_review: "tier.review()",
          validated: true,
          can_review: false,
          show_task_button: false,
          show_project_button: false,
          show_create_project_button: false,
          project_ids: [],
          is_product_milestone: false,
          project_count: 0,
          milestone_count: 0,
          closed_task_count: 0,
          tasks_count: 0,
          completed_task_percentage: 0,
          dropship_picking_count: 0,
          delivery_count: 1,
          invoice_count: 0,
          spreadsheet_template_id: false,
          spreadsheet_id: false,
          mrp_production_count: 0,
          purchase_order_count: 0,
          attendee_count: 0,
          name: "S00547",
          partner_id: {
            id: 10,
            display_name:
              "Deco Addict\n77 Santa Barbara Rd\nPleasant Hill CA 94523\n美国 ‒ US12345673",
          },
          delivery_set: false,
          is_all_service: false,
          recompute_delivery_price: false,
          grid: false,
          grid_product_tmpl_id: false,
          grid_update: false,
          partner_invoice_id: { id: 10, display_name: "Deco Addict" },
          partner_shipping_id: { id: 10, display_name: "Deco Addict" },
          sale_order_template_id: false,
          validity_date: "2025-08-20",
          date_order: "2025-07-21 12:42:57",
          has_active_pricelist: true,
          show_update_pricelist: false,
          pricelist_id: { id: 2, display_name: "默认 (USD)" },
          country_code: "US",
          company_id: { id: 1, display_name: "My Company (San Francisco)" },
          currency_id: { id: 1 },
          tax_country_id: { id: 233 },
          tax_calculation_rounding_method: "round_per_line",
          payment_term_id: { id: 4, display_name: "30 天" },
          order_line: [
            {
              id: 1142,
              sequence: 10,
              display_type: false,
              product_uom_category_id: { id: 1 },
              product_type: "consu",
              product_updatable: false,
              is_downpayment: false,
              product_id: {
                id: 2828,
                display_name: "11111111",
                name: "套件测试new11111111",
              },
              product_template_id: {
                id: 2524,
                display_name: "套件测试new1234",
              },
              event_id: false,
              event_ticket_id: false,
              product_add_mode: "configurator",
              product_template_attribute_value_ids: [],
              product_custom_attribute_value_ids: [],
              product_no_variant_attribute_value_ids: [],
              is_configurable_product: false,
              linked_line_id: false,
              virtual_id: false,
              linked_virtual_id: false,
              selected_combo_items: false,
              combo_item_id: false,
              name: "套件测试new",
              route_id: false,
              is_reward_line: false,
              product_uom_qty: 1,
              qty_delivered: 0,
              virtual_available_at_date: 0,
              qty_available_today: 0,
              free_qty_today: 0,
              scheduled_date: false,
              forecast_expected_date: false,
              warehouse_id: { id: 1 },
              move_ids: [1914, 1915, 1916],
              qty_to_deliver: 1,
              is_mto: false,
              display_qty_widget: false,
              qty_delivered_method: "stock_move",
              qty_invoiced: 0,
              qty_to_invoice: 1,
              product_uom_readonly: true,
              product_uom: { id: 1, display_name: "件" },
              customer_lead: 0,
              product_packaging_qty: 0,
              product_packaging_id: false,
              recompute_delivery_price: false,
              is_delivery: false,
              price_unit: 50,
              price_subtotal: 100,
              purchase_price: 0,
              margin: 1,
              margin_percent: 1,
              technical_price_unit: 1,
              tax_id: [{ id: 1, display_name: "15%" }],
              discount: 0,
              price_total: 1.15,
              tax_calculation_rounding_method: "round_per_line",
              state: "sale",
              invoice_status: "to invoice",
              currency_id: { id: 1 },
              price_tax: 0.1499999999999999,
              company_id: { id: 1 },
              product_document_ids: [],
              is_product_archived: false,
              service_tracking: "no",
              order_id: { id: 571 },
              available_product_document_ids: [],
              invoice_lines: [],
            },
          ],
          is_tax_computed_externally: false,
          note: '<p>Terms &amp; Conditions: <a href="http://localhost:5059/terms" target="_blank" rel="noreferrer noopener">http://localhost:5059/terms</a></p>',
          tax_totals: {
            currency_id: 1,
            currency_pd: 0.01,
            company_currency_id: 1,
            company_currency_pd: 0.01,
            has_tax_groups: true,
            subtotals: [
              {
                tax_groups: [
                  {
                    id: 1,
                    involved_tax_ids: [1],
                    tax_amount_currency: 0.15,
                    tax_amount: 0.15,
                    base_amount_currency: 1,
                    base_amount: 1,
                    display_base_amount_currency: 1,
                    display_base_amount: 1,
                    group_name: "税率15%",
                    group_label: false,
                  },
                ],
                tax_amount_currency: 0.15,
                tax_amount: 0.15,
                base_amount_currency: 1,
                base_amount: 1,
                name: "未税金额",
              },
            ],
            base_amount_currency: 1,
            base_amount: 1,
            tax_amount_currency: 0.15,
            tax_amount: 0.15,
            same_tax_base: true,
            total_amount_currency: 1.15,
            total_amount: 1.15,
          },
          margin: 1,
          amount_untaxed: 1,
          margin_percent: 1,
          loyalty_data: false,
          sale_order_option_ids: [],
          quotation_document_ids: [],
          customizable_pdf_form_fields: false,
          user_id: { id: 2, display_name: "Mitchell Admin" },
          team_id: { id: 1, display_name: "销售" },
          website_id: false,
          is_abandoned_cart: false,
          cart_recovery_email_sent: false,
          require_signature: true,
          require_payment: true,
          prepayment_percent: 1,
          reference: false,
          client_order_ref: false,
          tag_ids: [],
          is_avatax: false,
          avatax_unique_code: "Sales Order 571",
          report_grids: true,
          show_update_fpos: false,
          fiscal_position_id: false,
          project_id: false,
          journal_id: false,
          invoice_status: "to invoice",
          warehouse_id: { id: 1, display_name: "YourCompany" },
          incoterm: false,
          incoterm_location: false,
          picking_policy: "direct",
          shipping_weight: 0,
          commitment_date: false,
          expected_date: "2025-07-21 12:42:57",
          show_json_popover: false,
          json_popover:
            '{"popoverTemplate": "sale_stock.DelayAlertWidget", "late_elements": []}',
          effective_date: false,
          delivery_status: "pending",
          origin: false,
          campaign_id: false,
          medium_id: false,
          source_id: false,
          signed_by: false,
          signed_on: false,
          signature: false,
          write_date: "2025-07-21 12:42:56",
          has_archived_products: false,
          company_price_include: "tax_excluded",
          is_pdf_quote_builder_available: true,
          display_name: "S00547",
        },
      ];
      function flattenObject(obj, prefix = "") {
        const result = {};
        for (const key in obj) {
          const value = obj[key];
          const newKey = prefix ? `${prefix}_${key}` : key;

          if (
            typeof value === "object" &&
            value !== null &&
            !Array.isArray(value)
          ) {
            if ("id" in value || "display_name" in value) {
              if ("id" in value) result[`${newKey}_id`] = value.id;
              if ("display_name" in value)
                result[`${newKey}_display_name`] = value.display_name; // ← 修改点
            } else {
              Object.assign(result, flattenObject(value, newKey));
            }
          } else if (!Array.isArray(value)) {
            result[newKey] = value;
          }
        }
        return result;
      }
      function flattenArrayField(arr) {
        return arr.map((item) => {
          const row = {};
          for (const key in item) {
            const value = item[key];

            if (Array.isArray(value)) {
              continue; // 跳过二级数组
            } else if (typeof value === "object" && value !== null) {
              Object.assign(row, flattenSingleLevelObject(value, key));
            } else {
              row[key] = value;
            }
          }
          return row;
        });
      }
      function flattenSingleLevelObject(obj, prefix) {
        const result = {};
        for (const key in obj) {
          const value = obj[key];
          if (
            typeof value !== "object" ||
            value === null ||
            Array.isArray(value)
          ) {
            result[`${prefix}_${key}`] = value;
          }
        }
        return result;
      }
      function transformData(data) {
        const result = {};
        const firstObj = data[0];

        for (const key in firstObj) {
          const value = firstObj[key];

          if (Array.isArray(value)) {
            result[key] = flattenArrayField(value);
          } else if (typeof value === "object" && value !== null) {
            if ("id" in value || "display_name" in value) {
              if ("id" in value) result[`${key}_id`] = value.id;
              if ("display_name" in value)
                result[`${key}_display_name`] = value.display_name; // ← 修改点
            } else {
              Object.assign(result, flattenObject(value, key));
            }
          } else {
            result[key] = value;
          }
        }

        return result;
      }

      // 使用示例
      let printData1 = transformData(shuju);
      console.log(printData1);
      var configPrintJson = {
        panels: [
          {
            index: 0,
            height: 297,
            width: 210,
            paperType: "A4",
            paperHeader: 0,
            paperFooter: 805,
            printElements: [],
            paperNumberLeft: 565,
            paperNumberTop: 819,
          },
        ],
      };
      var configElementTypeProvider = (function () {
        return function (options) {
          var addElementTypes = function (context) {
            context.addPrintElementTypes("testModule", [
              new hiprint.PrintElementTypeGroup("业务数据", configData),
              new hiprint.PrintElementTypeGroup("专业", [
                {
                  tid: "configModule.professional",
                  title: "专业",
                  field: "professional",
                  data: "信息管理与信息系统",
                  type: "text",
                },
                {
                  tid: "configModule.university",
                  title: "大学",
                  field: "university",
                  data: "北京邮电大学",
                  type: "text",
                },
                {
                  tid: "configModule.universityAddress",
                  title: "地点",
                  field: "universityAddress",
                  data: "北京",
                  type: "text",
                },
                {
                  tid: "configModule.universityDate",
                  title: "时间",
                  field: "universityDate",
                  data: "2008",
                  type: "text",
                },
                {
                  tid: "configModule.tech",
                  title: "技能",
                  field: "tech",
                  data: "MYSQL,HIVE(数据仓库工具),SPSS(统计产品已服务解决方案),数据挖掘，跨部门沟通能力，业务理解能力，数据解读分析。",
                  type: "longText",
                },
              ]),
              new hiprint.PrintElementTypeGroup("辅助", [
                {
                  tid: "configModule.hline",
                  title: "横线",
                  type: "hline",
                },
                {
                  tid: "configModule.vline",
                  title: "竖线",
                  type: "vline",
                },
                {
                  tid: "configModule.rect",
                  title: "矩形",
                  type: "rect",
                },
                {
                  tid: "testModule.oval",
                  title: "椭圆",
                  type: "oval",
                },
              ]),
            ]);
          };

          return {
            addElementTypes: addElementTypes,
          };
        };
      })();
      $(function () {
        // 自定义工具栏（可以按需裁剪或修改）
        hiprint.init({
          providers: [new configElementTypeProvider()],
        });

        hiprint.PrintElementTypeManager.build(
          ".hiprintEpContainer",
          "testModule"
        );

        designer = new hiprint.PrintTemplate({
          template: configPrintJson,
          settingContainer: "#printElementOptionSetting",
        });

        // 渲染设计器
        designer.design("#printTemplate");
      });

      function getJsonTid() {
        console.log(designer.getJsonTid());
        console.log(
          new hiprint.PrintTemplate({
            template: designer.getJsonTid(),
          }).getHtml()
        );
      }
      function yulan() {
        var testTemplate = new hiprint.PrintTemplate({
          template: designer.getJsonTid(),
        });

        $("#yulan").html(testTemplate.getHtml(printData1));
      }
      function dayin() {
        var testTemplate = new hiprint.PrintTemplate({
          template: designer.getJsonTid(),
        });

        testTemplate.print(printData1);
      }
      function transformFields(fields) {
        const normalFields = [];
        const tableMap = {};

        function getTopParent(field) {
          while (field.parent) {
            field = field.parent;
          }
          return field;
        }

        for (const field of fields) {
          const id = field.id;

          // 普通字段（无 '/' 且类型不是 many2one/one2many/many2many）
          if (
            !id.includes("/") &&
            (!field.field_type || !field.field_type.includes("2many"))
          ) {
            normalFields.push({
              tid: `configModule.${id}`,
              title: field.string.split("/").pop(),
              field: id,

              data: "",
              type: "text",
            });
          }
        }

        for (const field of fields) {
          const id = field.id;

          // 嵌套字段（带 '/'）
          if (id.includes("/")) {
            const parts = id.split("/");
            const lastPart = parts[parts.length - 1];

            // 查找最顶层 parent，如果是 one2many 或 many2many，属于表格字段
            if (field.parent) {
              const top = getTopParent(field);
              const tableId = top.id;

              if (["one2many", "many2many"].includes(top.field_type)) {
                tableMap[tableId] = tableMap[tableId] || {
                  tid: `configModule.${tableId}`,
                  field: tableId,
                  title: top.string,
                  type: "table",
                  editable: true,
                  columnDisplayEditable: true,
                  columnDisplayIndexEditable: true,
                  columnTitleEditable: true,
                  columnResizable: true,
                  columnAlignEditable: true,
                  columns: [[]],
                };

                //const fieldKey = id.replace(/\//g, "_").replace(/\./g, "");
                const parts = id.split("/");
                const fieldKey = parts.slice(1).join("_"); // 去掉顶层字段（如 order_line）
                const fieldTitle = field.string.split("/").pop();

                tableMap[tableId].columns[0].push({
                  title: fieldTitle,
                  align: "center",
                  field: fieldKey,
                  width: 100,
                });
              } else {
                // 顶层不是 one2many 表格字段，直接作为普通字段处理
                normalFields.push({
                  tid: `configModule.${id.replace(/\//g, "_")}`,
                  title: field.string.split("/").pop(),
                  field: id.replace(/\//g, "_"),
                  data: "",
                  type: "text",
                });
              }
            } else {
              // 无 parent 直接保留为普通字段
              normalFields.push({
                tid: `configModule.${id.replace(/\//g, "_")}`,
                title: field.string.split("/").pop(),
                field: id.replace(/\//g, "_"),
                data: "",
                type: "text",
              });
            }
          }
        }

        return [...normalFields, ...Object.values(tableMap)];
      }

      // 构建 specification
      function buildSpecification(fields) {
        const spec = {};

        for (const field of fields) {
          const path = field.id.split("/");
          if (path.length === 1) {
            // 顶层字段
            spec[field.id] = {};
          } else {
            // 嵌套字段
            const [parent, child] = path;
            if (!spec[parent]) {
              spec[parent] = { fields: {} };
            }
            if (!spec[parent].fields) {
              spec[parent].fields = {};
            }
            spec[parent].fields[child] = {};
          }
        }

        return spec;
      }

      // 构建 payload
      function buildPayload(fields, id = 571) {
        return {
          id: 81,
          jsonrpc: "2.0",
          method: "call",
          params: {
            model: "sale.order",
            method: "web_read",
            args: [[id]],
            kwargs: {
              context: {
                lang: "zh_CN",
                tz: "Asia/Shanghai",
                uid: 2,
                abcdefg: true,
                allowed_company_ids: [1],
                bin_size: true,
              },
              specification: buildSpecification(fields),
            },
          },
        };
      }

      // 请求
      async function sendWebReadRequest() {
        const payload = buildPayload(abc1);

        const res = await fetch(
          "http://localhost:5059/web/dataset/call_kw/sale.order/web_read",
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify(payload),
          }
        );

        printData1 = await res.json();
        printData1 = transformData(printData1.result);
        console.log("返回结果：", printData1);
      }

      sendWebReadRequest();
    </script>
  </body>
</html>
